import axios from 'axios'

// 创建axios实例
const api = axios.create({
  baseURL: 'http://localhost:18090', // 直连订阅服务
  timeout: 10000, // 增加超时时间
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => {
    const res = response.data
    // 后端使用R类包装，成功时code为0
    if (res.code === 0) {
      return res.data
    } else {
      throw new Error(res.msg || '请求失败')
    }
  },
  error => {
    console.error('API Error:', error)
    // 处理网络错误或HTTP状态码错误
    const message = error.response?.data?.msg || error.message || '网络错误'
    throw new Error(message)
  }
)

// 订阅任务API
export const taskApi = {
  // 分页查询任务
  getTaskList(params) {
    return api.get('/api/subscription/tasks', { params })
  },

  // 根据ID获取任务
  getTaskById(id) {
    return api.get(`/api/subscription/tasks/${id}`)
  },

  // 创建或更新任务
  saveTask(data) {
    return api.post('/api/subscription/tasks', data)
  },

  // 删除任务
  deleteTask(id) {
    return api.delete(`/api/subscription/tasks/${id}`)
  },

  // 触发下发
  dispatchTask(id) {
    return api.post(`/api/subscription/tasks/${id}/dispatch`)
  }
}

// 订阅模板API
export const templateApi = {
  // 分页查询模板
  getTemplateList(params) {
    return api.get('/api/subscription/templates', { params })
  },

  // 根据ID获取模板
  getTemplateById(id) {
    return api.get(`/api/subscription/templates/${id}`)
  },

  // 创建或更新模板
  saveTemplate(data) {
    return api.post('/api/subscription/templates', data)
  },

  // 删除模板
  deleteTemplate(id) {
    return api.delete(`/api/subscription/templates/${id}`)
  }
}

// 健康检查API
export const healthApi = {
  check() {
    return api.get('/api/health')
  }
}

// 数据检控API
export const dataControlApi = {
  // 规则分组管理
  getGroupTree() {
    return api.get('/api/datacontrol/groups/tree')
  },

  getGroupDetail(id) {
    return api.get(`/api/datacontrol/groups/${id}`)
  },

  createGroup(data) {
    return api.post('/api/datacontrol/groups', data)
  },

  updateGroup(id, data) {
    return api.put(`/api/datacontrol/groups/${id}`, data)
  },

  deleteGroup(id) {
    return api.delete(`/api/datacontrol/groups/${id}`)
  },

  // 检控规则管理
  getRuleList(params) {
    return api.get('/api/datacontrol/rules', { params })
  },

  getRuleDetail(id) {
    return api.get(`/api/datacontrol/rules/${id}`)
  },

  createRule(data) {
    return api.post('/api/datacontrol/rules', data)
  },

  updateRule(id, data) {
    return api.put(`/api/datacontrol/rules/${id}`, data)
  },

  deleteRule(id) {
    return api.delete(`/api/datacontrol/rules/${id}`)
  },

  publishRule(id) {
    return api.post(`/api/datacontrol/rules/${id}/publish`)
  },

  unpublishRule(id) {
    return api.post(`/api/datacontrol/rules/${id}/unpublish`)
  },

  enableRule(id) {
    return api.post(`/api/datacontrol/rules/${id}/enable`)
  },

  disableRule(id) {
    return api.post(`/api/datacontrol/rules/${id}/disable`)
  },

  // 检控执行
  executeAutoControl() {
    return api.post('/api/datacontrol/execution/auto')
  },

  executeManualControl(ruleIds) {
    return api.post('/api/datacontrol/execution/manual', ruleIds)
  },

  getExecutionHistory(params) {
    return api.get('/api/datacontrol/execution/history', { params })
  },

  getExecutionDetail(executionId) {
    return api.get(`/api/datacontrol/execution/detail/${executionId}`)
  }
}

export default api
